package com.cxria.springboottest.repository.factory;

import com.cxria.springboottest.repository.impl.BaseRepositoryImpl;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;

import javax.persistence.EntityManager;
import java.io.Serializable;

public class BaseRepositoryFactory<T, ID extends Serializable> extends JpaRepositoryFactory {

	private final EntityManager em;

	public BaseRepositoryFactory(EntityManager em) {
		super(em);
		this.em = em;
	}

	// 设置具体的实现类是BaseRepositoryImpl
	@Override
	protected BaseRepositoryImpl<T, ID> getTargetRepository(RepositoryInformation information) {
		return new BaseRepositoryImpl<T, ID>((Class<T>) information.getDomainType(), em);
	}

	// 设置具体的实现类的class
	@Override
	protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
		return BaseRepositoryImpl.class;
	}
}
